package com.thtn.greendroidthtneg2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.AsyncTask;

public class DBAdapter extends SQLiteOpenHelper {

	private static String DB_NAME = "kdatabase";
	private static String DB_PATH;
	private static String DB_Table = "kreatorz";
	private static String Favorite_Table = "favorite";
	public static final String KEY_DEFINITION = "suggest_text_def";
	public static final String KEY_WORD = "suggest_text_1";
	public static String PACKAGE_NAME = "com.thtn.greendroidthtneg2";
	private static final String TAG = "DictionaryDatabase";
	private Context myContext;
	private SQLiteDatabase myDataBase;

	static {
		DB_PATH = "/data/data/" + PACKAGE_NAME + "/databases/";
	}

	public DBAdapter(Context paramContext) {
		super(paramContext, DB_NAME, null, 1);
		this.myContext = paramContext;
		createDataBase();
	}

	private void createDataBase() {
		if (checkDataBase()) {
			openDataBase();
			return;
		}
		getWritableDatabase();
		try {
			new DatabaseCopyTask().execute(new String[] { "" });
			return;
		} catch (Exception localException) {
		}
	}

	private boolean checkDataBase() {
		return new File(DB_PATH + DB_NAME).exists();
	}

	private void copyDataBase() throws IOException {
		InputStream localInputStream = getClass().getResourceAsStream(
				"/res/raw/" + DB_NAME);
		FileOutputStream localFileOutputStream = new FileOutputStream(DB_PATH
				+ DB_NAME);
		byte[] arrayOfByte = new byte[1024];
		while (true) {
			int i = localInputStream.read(arrayOfByte);
			if (i <= 0) {
				localFileOutputStream.flush();
				localFileOutputStream.close();
				localInputStream.close();
				return;
			}
			localFileOutputStream.write(arrayOfByte, 0, i);
		}
	}

	public Cursor bookmarkList(String paramString) {
		String str = "favorite='fav'" + paramString.trim();
		Cursor localCursor = this.myDataBase.query(true, DB_Table,
				new String[] { "_id", "suggest_text_1" }, str, null, null,
				null, "suggest_text_1 COLLATE NOCASE", null);
		localCursor.moveToFirst();
		return localCursor;
	}

	public void close() {
		try {
			if (this.myDataBase != null)
				this.myDataBase.close();
			super.close();
			return;
		} finally {
		}
	}

	public int delete(Uri paramUri, String paramString,
			String[] paramArrayOfString) {
		this.myDataBase.delete(DB_Table, "_id='" + paramString + "'", null);
		return 1;
	}

	public Cursor getRandom() {
		Cursor localCursor = this.myDataBase.query(DB_Table,
				new String[] { "_id" }, null, null, null, null, null, null);
		localCursor.moveToFirst();
		return localCursor;
	}

	public Cursor getSuggestions(String paramString) {
		String str1 = paramString.replaceAll("'", "''");
		if (str1.trim().length() < 1)
			return null;
		String str2 = "suggest_text_1 like'" + str1.toLowerCase() + "%'";
		Cursor localCursor = this.myDataBase.query(true, DB_Table,
				new String[] { "_id", "suggest_text_1",
						"suggest_intent_data_id" }, str2, null, null, null,
				"suggest_text_1 COLLATE NOCASE", null);
		localCursor.moveToFirst();
		return localCursor;
	}

	public Cursor getWord(Uri paramUri) {
		String str1 = paramUri.getLastPathSegment();
		String str2 = "_id='" + str1 + "'";
		Cursor localCursor = this.myDataBase
				.query(true, DB_Table, new String[] { "suggest_text_1",
						"suggest_text_def", "favorite" }, str2, null, null,
						null, null, null);
		localCursor.moveToFirst();
		return localCursor;
	}

	public Uri insert(Uri paramUri, ContentValues paramContentValues) {
		long l = this.myDataBase.insert(DB_Table, "favorite", null);
		paramContentValues.put("_id", l);
		paramContentValues.put("suggest_intent_data_id", l);
		this.myDataBase.update(DB_Table, paramContentValues, "_id='" + l + "'",
				null);
		return paramUri;
	}

	public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
	}

	public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1,
			int paramInt2) {
	}

	public void openDataBase() {
		this.myDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
				0);
	}

	public void updateRowData(String paramString,
			ContentValues paramContentValues) {
		this.myDataBase.update(DB_Table, paramContentValues, paramString, null);
	}

	public Cursor wordList(String paramString) {
		// TOdo
		return null;

	}

	private class DatabaseCopyTask extends AsyncTask<String, String, String> {
		private DatabaseCopyTask() {
		}

		protected String doInBackground(String[] paramArrayOfString) {
			try {
				DBAdapter.this.copyDataBase();
				return "Success";
			} catch (IOException localIOException) {
				System.out.println("Error copying database.");
				localIOException.printStackTrace();
			}
			return null;
		}

		protected void onPostExecute(String paramString) {
			if (paramString != null)
				DBAdapter.this.openDataBase();
			SearchableDictionary.COPYING_DATABASE = false;
		}

		protected void onPreExecute() {
			SearchableDictionary.COPYING_DATABASE = true;
		}
	}

}
